package cn.skyquant.quant4j.jforex.sdk.strategy2;

import cn.skyquant.quant4j.sdk.enums.TradeDirection;
import cn.skyquant.quant4j.sdk.util.constant.EAConstants;
import cn.skyquant.quant4j.sdk.util.maths.MathsUtils;
import cn.skyquant.quant4j.sdk.util.time.CalendarUtils;
import com.dukascopy.api.Instrument;

import java.text.ParseException;
import java.util.Objects;

/**
 * 订单注解
 */
public final class OrderLabel {
    public final String name;
    public final int eaId;
    public final int configIndex;
    public final int seriedIndex;
    public final Instrument instrument;
    public final TradeDirection td;
    public final long time;
    public final boolean valid;

    public OrderLabel(String label) {
        String[] labelarr = label.split(EAConstants.split);
        if (labelarr != null && labelarr.length == 7) {
            this.name = labelarr[0];
            this.eaId = Integer.parseInt(labelarr[1]);
            this.configIndex = Integer.parseInt(labelarr[2]);
            this.instrument = Instrument.valueOf(labelarr[3]);
            this.td = TradeDirection.parse(labelarr[4]);
            this.seriedIndex = Integer.parseInt(labelarr[5]);
            this.time = getTime(labelarr[6]);
            this.valid = true;
        } else {
            this.name = null;
            this.eaId = 0;
            this.time = 0;
            this.td = null;
            this.configIndex = 0;
            this.seriedIndex = 0;
            this.instrument = null;
            this.valid = false;
        }
    }

    public OrderLabel(AbstractConfig config, Instrument instrument, TradeDirection td, int seriedIndex, long time) {
        this.name = config.name;
        this.eaId = config.eaId;
        this.configIndex = config.index;
        this.seriedIndex = seriedIndex;
        this.instrument = instrument;
        this.td = td;
        this.time = time;
        this.valid = true;
    }

    public long getTime(String str) {
        long time = 0;
        try {
            time = CalendarUtils.parseGMTStandard(str).getTime();
        } catch (ParseException e) {
        }
        return time;
    }

    @Override
    public String toString() {
        return name + EAConstants.split
                + eaId + EAConstants.split
                + configIndex + EAConstants.split
                + instrument.name() + EAConstants.split
                + td.shortstr + EAConstants.split
                + seriedIndex + EAConstants.split
                + CalendarUtils.formatGMTStandard(time) + EAConstants.split
                + MathsUtils.getRandom(1000);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrderLabel orderLabel = (OrderLabel) o;
        return eaId == orderLabel.eaId &&
                configIndex == orderLabel.configIndex &&
                time == orderLabel.time &&
                seriedIndex == orderLabel.seriedIndex &&
                name.equals(orderLabel.name) &&
                instrument.equals(orderLabel.instrument) &&
                td == orderLabel.td;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, eaId, configIndex, instrument, td, time, seriedIndex);
    }
}
